package com.winit.uCloud.web;

import com.alibaba.fastjson.JSONObject;
import com.winit.uCloud.manager.UCVersionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by wangjian on 16/11/11.
 */
@RestController
@RequestMapping("/uCloud")
public class UCVersionController {

    private static Logger logger = LoggerFactory.getLogger(UCVersionController.class);

    @Autowired
    private UCVersionManager ucVersionManager;

    /**
     * 获取ucloud初始化时应用的版本
     * /beetle/uCloud/getProjectVersions?apps=sms,beetle-test,oms2
     *
     * @param request
     * @return
     */
    @RequestMapping("/getProjectVersions")
    @ResponseBody
    public Map<String, Object> getProjectVersions(HttpServletRequest request) {
        Map<String, Object> rtnMap = new HashMap<>();
        String apps = request.getParameter("apps");
        logger.info("获取发布版本,应用列表:" + apps);
        if (StringUtils.isEmpty(apps)) {
            rtnMap.put("code", "1");
            rtnMap.put("desc", "应用列表为空");
            return rtnMap;
        }
        List<JSONObject> list = ucVersionManager.getProjectVersions(apps);
        rtnMap.put("code", "0");
        rtnMap.put("data", list);
        return rtnMap;
    }

    /**
     * 获取工程发布版本
     * /beetle/uCloud/getProjectReleaseVersions?apps=['sms','beetle-test','oms2']
     *
     * @param request
     * @return
     */
    @RequestMapping("/getProjectReleaseVersions")
    @ResponseBody
    public Map<String, Object> getProjectReleaseVersions(HttpServletRequest request) {
        Map<String, Object> rtnMap = new HashMap<>();
        String apps = request.getParameter("apps");
        String type = request.getParameter("type");
        logger.info("获取工程线上版本,应用列表:" + apps);
        if (StringUtils.isEmpty(apps)) {
            rtnMap.put("code", "1");
            rtnMap.put("desc", "应用列表为空");
            return rtnMap;
        }
        List<JSONObject> list = ucVersionManager.getProjectOnlineVersions(apps,type);
        rtnMap.put("code", "0");
        rtnMap.put("data", list);
        return rtnMap;
    }
}
